package com.sx.art2.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sx.art2.beans.Admin;
import com.sx.art2.beans.Art;
import com.sx.art2.beans.Category;
import com.sx.art2.beans.RukuInfo;
import com.sx.art2.beans.Stock;
import com.sx.art2.service.ArtService;
import com.sx.art2.service.CategoryService;
import com.sx.art2.service.RukuInfoService;
import com.sx.art2.service.StockService;

   @Controller
public class StockController {
	@Autowired
	RukuInfoService rukuInfoService;
	@Autowired
	CategoryService  categoryService;
    @Autowired
    ArtService artService;
	@Autowired
   StockService stockService;
	@RequestMapping("/selectByartAdddate")
	 public ModelAndView list2(String artAdddate) {
		ModelAndView mv = new ModelAndView();
		List<Stock> list2=stockService.list2(artAdddate);
		mv.addObject("list2", list2);
		mv.setViewName("admin/stocklist1");
			return mv;	   
	   }
	@RequestMapping("/deletestock")
	@ResponseBody
	public String deleteStock(int stockId) {
		String date="success";
		stockService.delete(stockId);
		return date;
	}
	@RequestMapping("/addstock")
	public ModelAndView addStock(Stock stock, Art art, String categoryName, String artName, RukuInfo rukuInfo ,HttpSession session) {
		ModelAndView mv= new ModelAndView();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		art.setCategoryId(categoryService.selectByName(categoryName));
		art.setArtAdddate(df.format(new Date()));
		artService.insert(art);
		stock.setArtId(artService.selectByName(artName));
		stock.setArtAdddate(df.format(new Date()));
		stockService.add(stock);
		Admin admin=(Admin) session.getAttribute("Admin");
		rukuInfo.setAdminName(admin.getAdminName());
		rukuInfo.setArtAdddate(df.format(new Date()));
		rukuInfoService.insert(rukuInfo);
		mv.setViewName("admin/addstock");

		return mv;
	}
	
	@RequestMapping("/liststock")
   public ModelAndView list() {
	ModelAndView mv = new ModelAndView();
	List<Stock> list=stockService.list();
	mv.addObject("list", list);
	mv.setViewName("admin/stocklist");
		return mv;	   
   }
   
	@RequestMapping("/selectByIdstock")
	public ModelAndView selectById(int id)
	{
		ModelAndView mv= new ModelAndView();
		
		Stock stock=stockService.selectById(id);
		mv.addObject("stock",stock);
		mv.setViewName("stockupdate");
	    return mv;
	}
	@RequestMapping("/updatestock")
	@ResponseBody
	public String update(Stock stock) {
	   String date="success";
		stockService.update(stock);
		return date;
		
	}
	
	
		
	
	}
   
